<template>
  <div class="dashboard-container">
    <div class="container">
      <div class="tableBar">
        <label style="margin-right: 10px">标题：</label>
        <el-input v-model="title"
                  placeholder="请填写标题"
                  style="width: 15%"
                  clearable
                  @clear="init"
                  @keyup.enter.native="initFun" />

        <label style="margin-right: 10px; margin-left: 20px">内容：</label>
        <el-input v-model="content"
                  placeholder="请填写关键字搜索"
                  style="width: 15%"
                  clearable
                  @clear="init"
                  @keyup.enter.native="initFun" />
        <el-button class="continue btnQuery"
                   icon="el-icon-search"
                   @click="init(true)">
          查询
        </el-button>
          <button class="btnClear" @click="onClear">
            <i class="el-icon-circle-close"></i>
            清空
          </button>

        <div class="tableLab">
          <el-button type="primary"
                     style="margin-left: 15px;background-color:#62C232;border:1px solid #62C232"
                     @click="addTrendstype('add')">
            + 新增动态
          </el-button>
        </div>
      </div>
            <div class="delBut non pl"
                @click="deleteHandle('批量', null)">批量删除</div>
      <el-table v-if="tableData.length"
                :data="tableData"
                stripe
                class="tableBox"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection"
                         width="25" />
        <el-table-column prop="title"
                         label="标题" />
        <el-table-column prop="image"
                         label="图片">
          <template slot-scope="{ row }">
            <el-image style="width: 80px; height: 40px; border: none; cursor: pointer"
                      :src="row.image">
              <div slot="error"
                   class="image-slot">
                <img src="./../../assets/noImg.png"
                     style="width: auto; height: 40px; border: none">
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="content"
                         label="内容" />
        <el-table-column prop="createTime"
                         label="创建时间" />
        <el-table-column prop="updateTime"
                         label="最后操作时间" />
        <el-table-column label="操作"
                         width="250"
                         align="center">
          <template slot-scope="scope">
            <el-button type="text"
                       size="small"
                       class="blueBug"
                       @click="addTrendstype(scope.row.id)">
              修改
            </el-button>
            <el-button type="text"
                       size="small"
                       class="delBut"
                       @click="deleteHandle('单删', scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Empty v-else :is-search="isSearch" />
      <el-pagination v-if="counts > 10"
                     class="pageList"
                     :page-sizes="[10, 20, 30, 40]"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="counts"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import HeadLable from '@/components/HeadLable/index.vue'
import { getTrendsPage, editTrends, deleteTrends } from '@/api/trends'
import InputAutoComplete from '@/components/InputAutoComplete/index.vue'
import Empty from '@/components/Empty/index.vue'
import { baseUrl } from '@/config.json'

@Component({
  name: 'TrendsType',
  components: {
    HeadLable,
    InputAutoComplete,
    Empty,
  },
})
export default class extends Vue {
  private title: any = ''
  private content: any = ''
  private counts: number = 0
  private page: number = 1
  private pageSize: number = 10
  private checkList: string[] = []
  private tableData: [] = []
  private trendsState = ''
  private trendsCategoryList = []
  private categoryId = ''
  private trendsStatus = ''
  private isSearch: boolean = false

  created() {
    this.init()
  }
  initProp(val) {
    this.title = val
    this.initFun()
  }

  initFun() {
    this.page = 1
    this.init()
  }

  private async init(isSearch?) {
    this.isSearch = isSearch
    await getTrendsPage({
      page: this.page,
      pageSize: this.pageSize,
      title: this.title || undefined,
      content: this.content || undefined,
      // status: this.trendsStatus,
    })
      .then((res) => {
        if (res.data.code === 1) {
          this.tableData = res.data && res.data.data && res.data.data.records
          this.counts = Number(res.data.data.total)
        }
      })
      .catch((err) => {
        this.$message.error('请求出错了：' + err.message)
      })
  }

  // 添加
  private addTrendstype(st: string) {
    if (st === 'add') {
      this.$router.push({ path: '/trends/add' })
    } else {
      this.$router.push({ path: '/trends/add', query: { id: st } })
    }
  }

  // 删除
  private deleteHandle(type: string, id: any) {
    if (type === '批量' && id === null) {
      if (this.checkList.length === 0) {
        return this.$message.error('请选择删除对象')
      }
    }
    this.$confirm('确认删除该动态, 是否继续?', '确定删除', {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      deleteTrends(type === '批量' ? this.checkList.join(',') : id)
        .then((res) => {
          if (res && res.data && res.data.code === 1) {
            this.$message.success('删除成功！')
            this.init()
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch((err) => {
          this.$message.error('请求出错了：' + err.message)
        })
    })
  }

  // 全部操作
  private handleSelectionChange(val: any) {
    let checkArr: any[] = []
    val.forEach((n: any) => {
      checkArr.push(n.id)
    })
    this.checkList = checkArr
  }

  private handleSizeChange(val: any) {
    this.pageSize = val
    this.init()
  }

  private handleCurrentChange(val: any) {
    this.page = val
    this.init()
  }
  //清空按钮
  onClear() {
    this.title = ''
    this.content = ''
  }
}
</script>
<style lang="scss">
.el-table-column--selection .cell {
  padding-left: 10px;
}
</style>
<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
    .container {
      background: #fff;
      position: relative;
      z-index: 1;
      padding: 30px 28px;
      border-radius: 4px;
      //查询黑色按钮样式
      .normal-btn {
        background: #333333;
        color: white;
        margin-left: 20px;
      }
      .tableBar {
        margin-bottom: 20px;

        .tableLab {
          display: inline-block;
          float: right;
        }
      }
      .pl {
        cursor: pointer;
        font-size: 14px;
        padding: 0px 0px 10px 0;
        color: $gray-2;
      }
      .tableBox {
        width: 100%;
        border: 1px solid $gray-5;
        border-bottom: 0;
      }
      .pageList {
        text-align: center;
        margin-top: 30px;
      }
    }
  }
}
.btnQuery {
  margin-left: 20px;
  border: 1px solid #02b6fd !important;
  background-color: #02b6fd !important;
}
.btnQuery:hover {
  background-color: #6cd5ff !important;
  border: 1px solid #6cd5ff !important;
}

.btnClear {
  width: 92.45px;
  height: 40.4px;
  color: #202020;
  border-radius: 4px;
  background-color: #ffffff !important;
  border: 1px solid #e0e0e0 !important;
  margin-left: 15px;
}
.btnClear .el-icon-circle-close {
  color: #202020 !important;
}
.btnClear:hover {
  background-color: #eaf9ff !important;
  border: 1px solid #3dc8ff !important;
  cursor: pointer;
}
</style>
